﻿/// <reference path="../../Context/js/jquery.min.min.js" />
/// <reference path="../..http://static.mpsoft.vn/Context/js/plugins/select2/select2.min.js" />
var _url = "http://" + window.location.host;
$(document).ready(function () {
    myDataTables();
});

function myDataTables() {
    var opt = {
        "sPaginationType": "full_numbers",
        "oLanguage": {
            "sSearch": "<span>Tìm kiếm:</span> ",
            "sInfo": "Showing <span>_START_</span> to <span>_END_</span> of <span>_TOTAL_</span> entries",
            "sLengthMenu": "_MENU_ <span>Số dòng trên 1 trang</span>"
        },
        'sDom': "lfrtip",
        'aoColumnDefs': [{ 'bSortable': false, 'aTargets': [0, 3], 'sWidth': "40px" },
            { 'bSortable': false, 'aTargets': [4], 'sWidth': "100px" },
            { 'bSortable': false, 'aTargets': [5], 'sWidth': "150px" },
        { 'bSortable': false, 'aTargets': [1], 'sWidth': "50px", 'mRender': function (data, type, row) { return '<input type="checkbox" name="check" value="' + row.CategoryID + '">'; } }]
    };
    opt.order = [[1, 'asc']];
    opt.bProcessing = true;
    opt.bServerSide = true;
    opt.sAjaxSource = _url + "/Services/Response.aspx?type=4";
    opt.bDestroy = true;
    opt.sAjaxDataProp = "aaData";
    opt.fnDrawCallback = function (oSettings) {
        if (oSettings.bSorted || oSettings.bFiltered) {
            try {
                for (var i = 0, iLen = oSettings.aiDisplay.length ; i < 11 ; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[i]].nTr).html(i + 1).addClass("td-stt");
                }
            } catch (ex) {

            }
        }
        var that = this;
        this.$('.label').click(function () {
            var _itemid = $(this).attr("title");
            var $label = $(this);
            CallMethod("ProdActive", "{itemid:" + _itemid + " }", function (data) {
                if (data.d == 1) {
                    if ($label.attr("data-act") == "1") {
                        $label.attr("data-act", 0);
                        $label.removeClass("label-satgreen");
                        $label.addClass("label-lightred");
                        $label.html("Inactive");
                    }
                    else {
                        $label.attr("data-act", 1);
                        $label.removeClass("label-lightred");
                        $label.addClass("label-satgreen");
                        $label.html("Active");
                    }
                }
            }, fail);
        });
        this.$('.tb-text').blur(function () {
            var _itemid = $(this).attr("title");
            var _value = $(this).val();
            CallMethod("ProdOrder", $.toJSON({ itemid: _itemid, value: _value }), function (data) {
                if (data.d == 1) {
                }
            }, fail);
        });

        this.$('.btn').click(function (ev) {
            if ($(this).hasClass("Delete")) {
                ev.preventDefault();
                CallMethod("ProdDelete", $.toJSON({ deletes: $(this).attr("href") }), function (data) {
                    if (data.d == 1) {
                        window.location.reload();
                    }
                }, fail);
            }
        });

        $('[rel=tooltip]').tooltip();
    };
    opt.aoColumns = [{ mData: null }, { mData: null },
        { mData: "ProductName" },
        {
            mData: null, sClass: "tb-active", mRender: function (data, type, row) {
                if (row.IsActive)
                    return "<span title='" + row.ProductID + "' class='label label-satgreen' data-act='1' >Active</span>";
                else
                    return "<span title='" + row.ProductID + "' class='label label-lightred' data-act='0'>Inactive</span>";
            }
        },
        {
            mData: null, mRender: function (data, type, row) {
                return '<input type="text" class="tb-text" title="' + row.ProductID + '" value="' + row.SortOrder + '" />';
            }
        },
        {
            mData: null, sClass: "tb-options", mRender: function (data, type, row) {
                return "<a href='?mod=product&form=2&itemid=" + row.ProductID + "' class='btn' rel='tooltip' title='Edit'><i class='fa fa-edit'></i></a>" +
                    "<a href='" + row.ProductID + "' class='btn Delete' rel='tooltip' title='Delete'><i class='fa fa-times'></i></a>";
            }
        }];
    if ($(".dataTable").length > 0) {
        var table = $(".dataTable").dataTable(opt);
        $(table).css("width", '100%');
        $('.dataTables_filter input').attr("placeholder", "Search here...").addClass("form-control input-medium");
        $(".dataTables_length select").wrap("<div class='input-mini'></div>").chosen({
            disable_search_threshold: 9999999
        });
        $("#check_all").click(function (e) {
            $('input', table.fnGetNodes()).prop('checked', this.checked);
        });
    }
    $("#cmdDelete").click(function (ev) {
        ev.preventDefault();
        var listChecked = $(".table tr td input[type='checkbox']");
        var _deletes = null;
        $.each(listChecked, function (index, obj) {
            if ($(obj).prop("checked")) {
                if (_deletes == null)
                    _deletes = $(obj).val();
                else
                    _deletes += "," + $(obj).val();
            }
        });
        if (_deletes != null) {
            CallMethod("CateDelete", $.toJSON({ deletes: _deletes }), function (data) {
                if (data.d == 1) {
                    window.location.reload();
                }
            }, fail);
        }
    });
}

function CallMethod(NameMethod, paramter, onSuccess, Fail) {
    $.ajax({
        type: "POST",
        url: _url + "/Services/Response.aspx/" + NameMethod,
        dataType: "json",
        data: paramter,
        contentType: "application/json; charset=utf-8",
        success: onSuccess,
        Error: Fail
    });
}
function fail(response) {
    alert("Có lỗi xảy ra yêu cầu làm lại.");
}